package cn.nubia.admin.sysconfig.service;

import cn.nubia.admin.sysconfig.bean.RoleResource;
import cn.nubia.admin.sysconfig.bean.SystemLog;
import cn.nubia.admin.sysconfig.query.RoleQueryParam;
import cn.nubia.admin.common.mybatis.bean.Query;
import cn.nubia.admin.common.mybatis.service.BaseServiceImpl;
import cn.nubia.admin.sysconfig.bean.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


/**
 * Created by Jason on 2015/6/27.
 */
@Service
public class RoleService extends BaseServiceImpl<Role> {

    @Autowired
    private RoleResourceService roleResourceService;

    @Autowired
    SystemLogService systemLogService;


    /**
     * 返回角色列表.
     *
     * @param roleQueryParam 查询条件
     * @return 带分页信息的角色列表.
     */
    public List<Role> findList(RoleQueryParam roleQueryParam) {
        Query<Role> query = Query.build(Role.class);
        query.setPaged(roleQueryParam.getPageNo(),roleQueryParam.getPageSize());
        List<Role> rolePaged = findByQuery(query);
        return rolePaged;
    }


    /**
     * 保存或者修改角色.
     *
     * @param role 角色对象
     */
    public void save(Role role) {
        role.setUpdateTime(new Date());
        if (null == role.getId()) {
            insert(role);
            systemLogService.log(SystemLog.ModuleTypeEnum.ROLELIST, SystemLog.OperateTypeEnum.ADD, role.getId());

        } else {
            update(role);
            systemLogService.log(SystemLog.ModuleTypeEnum.ROLELIST, SystemLog.OperateTypeEnum.EDIT, role.getId());

        }
    }


    /**
     * 获取角色资源列表.
     *
     * @param role 角色对象.
     * @return 角色资源列表.
     */
    public List<RoleResource> getRoleResourceList(Role role) {
        Query<RoleResource> query = Query.build(RoleResource.class);
        query.addEq("roleId", role.getId());
        return roleResourceService.findByQuery(query);
    }

    /**
     * 保存角色权限.
     *
     * @param roleResource 角色权限.
     * @return
     */
    public void saveRoleResource(RoleResource roleResource) {
        Query<RoleResource> roleResourceQuery = Query.build(RoleResource.class);
        roleResourceQuery.addEq("resId",roleResource.getResId());
        roleResourceQuery.addEq("roleId",roleResource.getRoleId());
        RoleResource resource1 = roleResourceService.get(roleResourceQuery);
        if (null == resource1) {
            roleResourceService.insert(roleResource);
            systemLogService.log(SystemLog.ModuleTypeEnum.ROLERESOURCELIST, SystemLog.OperateTypeEnum.EDIT, roleResource.getId());
        }
    }

    /**
     * 删除角色权限.
     * @param roleResource
     */
    public void deleteRoleResource(RoleResource roleResource) {
        Query<RoleResource> roleResourceQuery = Query.build(RoleResource.class);
        roleResourceQuery.addEq("resId",roleResource.getResId());
        roleResourceQuery.addEq("roleId",roleResource.getRoleId());
        RoleResource resource1 = roleResourceService.get(roleResourceQuery);
        if (null != resource1) {
            roleResourceService.delete(resource1);
            systemLogService.log(SystemLog.ModuleTypeEnum.ROLERESOURCELIST, SystemLog.OperateTypeEnum.DEL, roleResource.getId());

        }
    }
}
